package com.panshi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panshi.entity.AppUser;
import com.panshi.entity.SysAnswerCheck;
import com.panshi.mapper.AnswerMapper;
import com.panshi.entity.AppAnswer;
import com.panshi.service.AnswerService;
import com.panshi.service.FeignAppUserClient;
import com.panshi.service.FeignCommentClient;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 问题回答service
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired // 用户接口
    private FeignAppUserClient feignAppUserClient;


    @Override
    public List<AppAnswer> queryAnswerByMid(Integer matterId) {
        List<AppAnswer> list = answerMapper.queryAnswerByMid(matterId);
        Integer userId = null;
        for (AppAnswer a: list) {
            userId = a.getUserId();
            if(userId!= null && userId != 0 && userId.equals(a.getUserId())){
                // 查询回答用户信息
                AppUser appUser = feignAppUserClient.queryUserInfo(a.getUserId());
                a.setAppUser(appUser);
            }

        }
        return list;
    }

    @Override
    public DataResult queryAnswerMidPage(Integer matterId, PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<AppAnswer> list = answerMapper.queryAnswerByMid(matterId);
        Integer userId = null;
        for (AppAnswer a: list) {
            userId = a.getUserId();
            if(userId!= null && userId != 0 && userId.equals(a.getUserId())){
                // 查询回答用户信息
                AppUser appUser = feignAppUserClient.queryUserInfo(a.getUserId());
                a.setAppUser(appUser);
            }

        }
        PageInfo pageInfo = new PageInfo(list);
        return new DataResult().setCode(0).setData(pageInfo);
    }

    @Override
    public void updatePariseTread(Integer answerId, String code) {
        answerMapper.updatePariseTread(answerId, code);
    }

    @Override
    public void addAnswer(SysAnswerCheck sysAnswerCheck) {
        answerMapper.addAnswer(sysAnswerCheck);
    }

    @Override
    public void deleteAnswer(Integer answerId) {
        answerMapper.deleteAnswer(answerId);
    }

    @Override
    public AppAnswer queryInfo(Integer answerId) {
        return answerMapper.queryInfo(answerId);
    }


}
